import {HexPosition} from "@/components/hex/HexContainer";

/**
 * 根据立方体坐标，计算画布坐标
 * @param position 立方体坐标
 * @param center   立方体坐标[0,0,0]对应的画布位置
 * @param radius   外切圆半径
 * @param inclination 倾角
 */
export function transform(
    position: HexPosition,
    center: [number, number],
    radius: number,
    inclination: number,
): [number, number] {
    let x = 0;
    let y = 0;
    for (const [i, v] of position.entries()) {
        const angle = (Math.PI / 180) * -(i * 120 + inclination);
        x += v * radius * Math.cos(angle);
        y += v * radius * Math.sin(angle);
    }
    return [x + center[0], y + center[1]];
}

/**
 * 已知半径和倾角，计算六边形六个点的位置
 * @param center  圆心位置
 * @param radius  外切圆半径
 * @param inclination 倾角
 */
export function hexPointList(center: [number, number], radius: number, inclination: number): [number, number][] {
    return Array.from({length: 6}, (_, i) => {
        const angle = (Math.PI / 180) * -(i * 60 + inclination);
        const x = radius * Math.cos(angle)
        const y = radius * Math.sin(angle)
        return [center[0] + x, center[1] + y];
    });
}

export function hexPoints(center: [number, number], radius: number, inclination: number): string {
    return hexPointList(center, radius, inclination).map(a => a.join(',')).join(' ');
}